# 1/把昨天封装的Math类中的实例方法改为静态方法, 体会一下两者的区别
from math import sqrt


class Mymath:
#加
    @staticmethod
    def add(x, y):
        return x + y
#减
    @staticmethod
    def sub(x, y):
        return x - y
#乘
    @staticmethod
    def mul(x, y):
        return x * y
#除
    @staticmethod
    def dul(x, y):
        try:
            return x / y
        except Exception as e:
            print("错误信息如下：")
            print(e.args)
            return "除数不能为0！！"
#幂
    @staticmethod
    def power(x, y):
        return x ** y
#开方
    @staticmethod
    def math_sqrt(x):
        try:
            return sqrt(x)
        except Exception as e:
            print("错误信息如下：")
            print(e.args)
            return "只能对正数开方计算！！"



# 2/昨天大家将身边的事物抽象出一个类, 在此基础上添加以下功能:
# 在类中创建类属性
# 在类中创建私有属性
# 在__init__中初始化实例属性
# 在__init__中初始化实例私有属性
# 通过类方法来获取类的私有属性
# 在自己的类中实现__str__


class Car(object):
    car_length = "2.5M"
    __car_weight = "2000kg"

    def __init__(self, color, logo):
        self.color = color
        self.__logo = logo
#定义类方法
    @classmethod
    def get_car_weight(cls):
        return cls.__car_weight

# __str__方法
    def __str__(self):
        return "Your car is so cool!"


if __name__ == "__main__":
    print(f"5+2={Mymath.add(5, 2)}")
    print(f"5-2={Mymath.sub(5, 2)}")
    print(f"5*2={Mymath.mul(5, 2)}")
    print(f"5/2={Mymath.dul(5, 2)}")
    print(f"5的2次幂={Mymath.power(5, 2)}")
    print(f"16开方后={Mymath.math_sqrt(16)}")
    #获得类属性
    print(f"The length of the car is {Car.car_length}")
    # 获得类私有属性
    print(f"The weight of the car is {Car.get_car_weight()}")
    # 获得实例属性
    car = Car("red", "BWM" )
    print(f"The color of the car is {car.color}")
    # 获得实例私有属性
    print(f"The logo of the car is {car._Car__logo}")
    #str输出
    car1 = Car("green", "Benz")
    print(car1)

























# class Go():
#     field = "go"
#     __siyou = "s"
#
# print(Go.field)#类属性
# print(Go._Go__siyou)#类私有属性
#
#
# class Ch(Go):
#     weight = 80
#     __nation ="cn"
#     def __init__(self, rank, yin):
#         self.rank = rank
#         self.__yin = yin
#
#     # @classmethod
#     # def get_nation(cls):
#     #     return cls.__nation
#
#     def get__nation(self):
#         return self._Ch__nation
# ke = Ch(7, 99)
# print(ke.rank)#通过实例访问实例属性
# print(ke._Ch__yin)#访问实例的私有属性
# #print(Ch.rank) 绑定在实例上的属性不能通过类访问
#
# print(Ch._Ch__nation)
# # print(Ch.get_nation())#通过类方法访问类属性，不想通过实例调用类的属性
# print(ke.get__nation())
#
